import api from "./init";
import { AxiosResponse } from "axios";
import {Page, Result} from "@/types/api-common";
import { ImageType } from "@/types/file-upload";
import {UserProfileType} from "@/types/user-profile";

/**
 * @author BelovedLYC
 * @description 获取自己的信息
 */
export const getUserProfileByTokenApi = (): Promise<
  AxiosResponse<Result<any>>
> => {
  return api.get("/user/profile");
};

/**
 * @author BelovedLYC
 * @description 修改自己的信息
 */
export const updateUserProfileApi = (
  data: any
): Promise<AxiosResponse<Result<void>>> => {
  return api.put("/user/profile", data);
};

export const updateAvatarApi = (
  formData: FormData
): Promise<AxiosResponse<Result<ImageType>>> => {
  return api.post("/user/avatar", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

/**
 * @author BelovedLYC
 * @param id string
 * @description 用于新朋友页面获取新朋友基本信息
 *
 */
export const getUserProfileByID = (id: string) : Promise<AxiosResponse<Result<UserProfileType>>> => {
  return api.get(`/user/profile/${encodeURIComponent(id)}`);
};


/**
 * @author FlanChan
 * @description 通过UUID搜索用户
 * @param uuid 用户的UUID
 * @returns {Promise<AxiosResponse<Result<UserProfileType>>} 用户信息
 */
export const searchUserByUUID = (uuid: string): Promise<AxiosResponse<Result<UserProfileType>>> => {
  return api.get("/user/search/uuid", {
    params: { uuid },
  });
};

/**
 * @author FlanChan
 * @description 通过名称或邮箱搜索用户
 * @param keyword 搜索关键字
 * @param page 页码，默认值为1
 * @param size 每页数量，默认值为10
 * @param sort 排序方式，1:升序，2:降序，默认值为2
 * @returns {Promise<AxiosResponse<Result<PageVo<UserProfileType>>>} 用户分页信息
 */
export const searchUserByKeyword = (
    keyword: string,
    page: number = 1,
    size: number = 10,
    sort: number = 2
): Promise<AxiosResponse<Result<Page<UserProfileType>>>> => {
  return api.get("/user/search/keyword", {
    params: { keyword, page, size, sort },
  });
};